#include <locale>
#include <iostream>
#include "utility.h"

using namespace std;

string Input::tolower(const string& z){
	string s = "";
	for(auto it = z.begin(); it != z.end(); ++it){
		s.push_back(std::tolower(*it,locale()));
	}
	return s;
}

int Input::getNumber(){
	int k;
	string s;
	do{
		getline(cin,s);
		k = atoi(s.c_str());
	}while(k==0 && s!="0");
	return k;
}

string Input::getISBN(){
	string s;
	do{
		cin >> s;
		if(s[0]=='9' && s[1]=='7' && s[2]=='8' && s.length()==13) return s;
		cout << "Az ISBN formatuma nem megfelelo! Adja meg ujra!" << endl;
	}while(true);
}

string Crypt::encrypt(const string& s){
	const static string key="DivideEtImpera";
	string result = "";
	for(int i=0,j;i<(int)s.size();++i){
		j = i%key.length();
		int k;
		try{
			k=((NumberOf(s[i])+NumberOf(key[j]))%65);
		}catch(CharError){
			k=((NumberOf(' ')+NumberOf(key[j]))%65);
		}
		result.append(1,CharOf(k));
	}
	return result;
}

string Crypt::decrypt(const string& s){
	const static string key="DivideEtImpera";
	string result= "";
	for(int i=0,j;i<(int)s.size();++i){
		j = i%key.length();
		int k;
		try{
			k=((NumberOf(s[i])-NumberOf(key[j])+65)%65);
		}catch(CharError){
			k=10;
		}
		result.append(1,CharOf(k));
	}
	return result;
}

int Crypt::NumberOf(char ch){
	if(ch>=48 && ch<=57) return ch-48;
	if(ch==' ') return 10;
	if(ch>=65 && ch<=90) return ch-54;
	if(ch>=97 && ch<=122) return ch-60;
	if(ch=='@') return 63;
	if(ch=='.') return 64;
	throw CharError();
}

char Crypt::CharOf(int k){
	if(k<=9) return k+48;
	if(k==10) return ' ';
	if(k<=36) return k+54;
	if(k<=62) return k+60;
	if(k==63) return '@';
	if(k==64) return '.';
	throw CharError();
}


void WaitForEnter(){
	cin.clear();
	cout << "A folytatashoz usson ENTER-t...";
	char s; string z;
	getline(cin,z);
	cin.get(s);
}